<?php
namespace app\reduction\model;

use app\BaseModel;
use think\facade\Db;

class StaffRecord extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_reduction_staff_record';


    public $append = [
        //领取数量
        'get_num',
        //使用数量
        'use_num',
    ];

    /**
     * @author chenniang
     * @DataTime: 2020-04-21 13:16
     * @功能说明:领取数量
     */
    public function getGetNumAttr($value,$data){

        if(isset($data['staff_id'])&&isset($data['active_id'])){

            $dis = [

                'active_id' => $data['active_id'],

                'staff_id'  => $data['staff_id'],
            ];

            $user_record = new UserRecord();

            $num = $user_record->recordCount($dis);

            return $num;
        }
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-21 13:16
     * @功能说明:使用数量
     */
    public function getUseNumAttr($value,$data){

        if(isset($data['staff_id'])&&isset($data['active_id'])){

            $dis = [

                'active_id' => $data['active_id'],

                'staff_id'  => $data['staff_id'],

                'status'    => 2,
            ];

            $user_record = new UserRecord();

            $num = $user_record->recordCount($dis);

            return $num;
        }
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 10:23
     * @功能说明:员工发放记录列表
     */
    public function staffRecordList($dis,$mapor,$page){

        $data = $this->alias('a')
                ->join('longbing_card_user_info b','a.staff_id = b.fans_id')
                ->join('longbing_card_user c','a.staff_id = c.id')
                ->join('longbing_card_company d','a.company_id = d.id','left')
                ->join('longbing_card_company e','a.top_company_id = e.id','left')
                ->where($dis)
                ->where(function ($query) use ($mapor){
                    $query->whereOr($mapor);
                })->field(['a.*','b.name as staff_name','c.nickName','d.name as company_name','e.name as top_company_name'])
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['company_name'] = !empty($v['company_name'])?$v['company_name']:'暂无部门';

            }

        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 15:50
     * @功能说明:支付码列表
     */

    public function staffQrList($dis,$mapor,$page){

        $data = Db::name('longbing_card_user_info')->alias('a')
                ->join('longbing_card_user b','a.fans_id = b.id')
                ->join( 'longbing_card_company c', 'a.company_id = c.id', 'LEFT' )
                ->join( 'longbing_card_company d', 'c.top_id = d.id', 'LEFT' )
                ->where($dis)
                ->where(function ($query) use ($mapor){
                    $query->whereOr($mapor);
                })
                ->field(['b.id','a.name','b.reduction_qr','b.nickName','avatar as staff_img','b.avatarUrl as user_img','a.company_id','c.name as company_name','d.name as top_company_name'])
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                if(empty($v['top_company_name'])){

                    $v['top_company_name'] = $v['company_name'];

                    $v['company_name']     = '未设置部门';
                }

                $v['staff_img'] = !empty($v['staff_img'])?$v['staff_img']:$v['user_img'];

                $v['staff_img'] = !empty($v['staff_img'])?$v['staff_img']:"https://lbqnyv2.migugu.com/defaultAvatar.png";
            }
        }
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-22 11:16
     * @功能说明:分享记录详情
     */
    public function recordInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-22 13:34
     * @功能说明:添加发放记录
     */
    public function recordAdd($data,$staff_id){

        $company     = Db::name('longbing_card_user_info')->where(['fans_id'=>$staff_id])->value('company_id');

        $top_company = Db::name('longbing_card_company')->where(['id'=>$company])->value('top_id');

        if(empty($top_company)){

            $data['top_company_id'] = $company;

            $data['company_id']     = 0;
        }else{

            $data['top_company_id'] = $top_company;

            $data['company_id']     = $company;

        }

        $data['status'] = 1;

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;
    }















}